package com.binc.testspring.study.nio.netty.protocoltcp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.nio.charset.Charset;

/**
 * FileName: MyServerHandler
 * Autho: binC
 * Date:  2022/4/21 14:39
 */
public class MyServerHandler_object extends SimpleChannelInboundHandler<Object> {


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg1) throws Exception {
        System.out.println("从客户端" + ctx.channel().remoteAddress() + " 读取到long " + msg1);
        //获取信息
        ByteBuf body = (ByteBuf)msg1;
        MessageProtocol recHd = new MessageProtocol();
        int nLength = body.readInt();
        // int nType = body.readInt();
        int nDataSize = body.readableBytes();
        byte [] aa = new byte[nDataSize];
        body.readBytes(aa);
        String myMsg = new String(aa, Charset.forName("utf-8"));


        System.out.println("<<<<<<服务器收到消息:"+nLength+" "+myMsg+" <<<<<<<<<<<<<<<<<");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
